package com.alpha.demoretrofit;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alpha.demoretrofit.retrofit.MovieApiPost;
import com.alpha.demoretrofit.retrofit.rxjava.RxMovieApiGet;
import com.alpha.demoretrofit.retrofit2.MovieSubject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final String BASE_URL = "http://apis.juhe.cn";
    private static final String MOIVE_URL = "https://api.douban.com/v2/movie/";
    @Bind(R.id.button)
    Button button;
    @Bind(R.id.tv_ok)
    TextView tvOk;
    @Bind(R.id.tv_error)
    TextView tvError;
    @Bind(R.id.list_view)
    RecyclerView mRecyclerView;
    private MovieAdapter mMovieAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mRecyclerView.addItemDecoration(new MovieDecoration());
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        //设置参数
        mMovieAdapter = new MovieAdapter();
        mRecyclerView.setAdapter(mMovieAdapter);
        getMovieList();
    }

    /**
     * 获取电影列表
     */
    private void getMovieList() {
//        Network.getCityApi().getTop250(0, 10)
//                .map(new Func1<MovieSubject, List<Movie>>() {
//                    @Override
//                    public List<Movie> call(MovieSubject movieSubject) {
//                        return movieSubject.subjects;
//                    }
//                }).subscribe(new Action1<List<Movie>>() {
//            @Override
//            public void call(List<Movie> movies) {
//                mMovieAdapter.setMovies(movies);
//                mMovieAdapter.notifyDataSetChanged();
//            }
//        }, new Action1<Throwable>() {
//            @Override
//            public void call(Throwable throwable) {
//                Log.e("TAG", "error message:" + throwable.getMessage());
//                if (throwable instanceof Fault) {
//                    Fault fault = (Fault) throwable;
//                    if (fault.getErrorCode() == 404) {
//                        //错误处理
//                    } else if (fault.getErrorCode() == 500) {
//                        //错误处理
//                    } else if (fault.getErrorCode() == 501) {
//                        //错误处理
//                    }
//                }
//            }
//        });

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MOIVE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RxMovieApiGet rxMovieApiGet = retrofit.create(RxMovieApiGet.class);
        Observable<MovieSubject> top250 = rxMovieApiGet.getTop250(0, 20);
        top250.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<MovieSubject>() {
                    @Override
                    public void call(MovieSubject movieSubject) {
                        mMovieAdapter.setMovies(movieSubject.subjects);
                        mMovieAdapter.notifyDataSetChanged();
                    }
                });
    }

    public static class MovieDecoration extends RecyclerView.ItemDecoration {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.set(0, 0, 0, 20);
        }
    }


    @OnClick(R.id.button)
    public void onClick() {
        // 构建 Retrofit实例
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MOIVE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        // 构建接口的实例
        MovieApiPost movieApiGet = retrofit.create(MovieApiPost.class);
        Call<MovieSubject> call = movieApiGet.getTop250(0, 10);
        // call.execute();// 同步执行网络请求
        call.enqueue(new Callback<MovieSubject>() {// 异步执行网络请求,而且可以直接操作UI线程
            @Override
            public void onResponse(Call<MovieSubject> call, Response<MovieSubject> response) {
//                MovieSubject body = response.body();
//                tvOk.setText(body.title+"");
                mMovieAdapter.setMovies(response.body().subjects);
                mMovieAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<MovieSubject> call, Throwable t) {
                tvError.setText(t.toString());
            }
        });
    }
}
